    
    <section>
      <ul class="breadcrumb">
        <li><a href="<?php echo base_url('index.php/mainController/'); ?>">Home</a> <span class="divider">/</span></li>
        <li><a href="<?php echo base_url('index.php/employeeController/'); ?>">รายชื่อผู้ใช้งานในระบบ</a> <span class="divider">/</span></li>
        <li class="active">สร้างผู้ใช้งานระบบ</li>
      </ul>

      <div class="page-header">
        <h1>สร้างผู้ใช้งานระบบ</h1>
      </div>

      <script type="text/javascript">

      ////////////////////////////////////////////////
      function validateUsername(username){
        var reg = /^[a-z0-9]{4,30}$/;
        return username.match(reg);
      }

      function validateFirstname(firstname){
        var reg = /^[ก-๐a-zA-Z]+$/;
        return firstname.match(reg);
      }

      function validateLastname(lastname){
        var reg = /^[ก-๐a-zA-Z]+$/;
        return lastname.match(reg);
      }

      function validateGeneral(general){
        var reg = /^[ก-๐a-zA-Z0-9\s]+$/;
        return general.match(reg);
      }

      function validateTel(tel){
        var reg = /^0[2|8|9][0-9]{7,8}$/;
        return tel.match(reg);
      }

      function validatePassword(pwd){
        var reg = /^[a-zA-Z0-9]{8,30}$/;
        
        var resultValid = new Array();
        
        if(pwd.match(reg) != null){
          reg = /[a-z]/;  
          resultValid[0] = reg.test(pwd);
          reg = /[A-Z]/;  
          resultValid[1] = reg.test(pwd);
          reg = /[0-9]/;  
          resultValid[2] = reg.test(pwd);
          
          if(resultValid[0] == false || resultValid[1] == false || resultValid[2] == false){
            return null;
          }else{
            return true;
          }
        }
        return null;
      }
      ////////////////////////////////////////////////


      $(function(){
        var usrIsCorrect = true;
        var firstnameIsCorrect = true;
        var lastnameIsCorrect = true;
        var telIsCorrect = true;
        var passwordIsCorrect = true;
        var cormfirmPassIsCorrect = true;

        $('#username').bind('keyup',function(){
          if( $('#username').val() == "" ){
            $('#username').parent().parent().removeAttr('class');
            $('#username').parent().parent().addClass('control-group');
            $('#username').next().html('');
            return;
          }

          if(validateUsername($('#username').val()) == null){
            $('#username').parent().parent().removeAttr('class');
            $('#username').parent().parent().addClass('control-group error');
            $('#username').next().html('ชื่อในระบบไม่ถูกต้อง');
            usernameIsCorrect = false;
            return;
          }else{
            $('#username').parent().parent().removeAttr('class');
            $('#username').parent().parent().addClass('control-group success');
            $('#username').next().html('');
            firstnameIsCorrect = true;
          }

          $.ajax({
            type : 'post',
            url : 'checkUsername',
            data : { username : $(this).val() },
            success : function(result){
              //$('#test').html(result);
              $('#username').parent().parent().removeAttr('class');
              if (result.indexOf('notFound') > -1) {
                $('#username').parent().parent().addClass('control-group success');
                $('#username').next().html('ชื่อนี้สามารถใช้ได้ ');
                usrIsCorrect = true;
              }else{
                $('#username').parent().parent().addClass('control-group error');
                $('#username').next().html('มีผู้ใช้งานนี้แล้วในระบบ ');
                usrIsCorrect = false;
              }
            },
            error : function(err){
              alert('Error '+err);
            }
          });
        });

        $('#firstname').bind('keyup',function(){
          if(validateFirstname($('#firstname').val()) == null){
            
            $('#firstname').parent().parent().removeAttr('class');
            $('#firstname').parent().parent().addClass('control-group error');
            $('#firstname').next().html('ชื่อไม่ถูกต้อง');
            firstnameIsCorrect = false;
            
          }else{
            $('#firstname').parent().parent().removeAttr('class');
            $('#firstname').parent().parent().addClass('control-group success');
            $('#firstname').next().html('');
            firstnameIsCorrect = true;
          }
        });

        $('#lastname').bind('keyup',function(){
          if(validateLastname($('#lastname').val()) == null){
            
            $('#lastname').parent().parent().removeAttr('class');
            $('#lastname').parent().parent().addClass('control-group error');
            $('#lastname').next().html('นามสกุลไม่ถูกต้อง');
            formNotTrue = false;
          }else{
            $('#lastname').parent().parent().removeAttr('class');
            $('#lastname').parent().parent().addClass('control-group success');
            $('#lastname').next().html('');
            formNotTrue = true;
          }
        });

        $('#tel').bind('keyup',function(){

          if ($('#tel').val() == "" ) {
            $('#tel').parent().parent().removeAttr('class');
            $('#tel').parent().parent().addClass('control-group');
            telIsCorrect = true;
            return;
          }

          if(validateTel($('#tel').val()) == null){
            $('#tel').parent().parent().removeAttr('class');
            $('#tel').parent().parent().addClass('control-group error');
            $('#tel').next().html('เบอร์โทรศัพท์ไม่ถูกต้อง');
            telIsCorrect = false;
          }else{
            $('#tel').parent().parent().removeAttr('class');
            $('#tel').parent().parent().addClass('control-group success');
            $('#tel').next().html('');
            telIsCorrect = true;
          }
        });

        $('#password').bind('keyup',function(){
          if(validatePassword($('#password').val()) == null){
            $('#password').parent().parent().removeAttr('class');
            $('#password').parent().parent().addClass('control-group error');
            $('#password').next().html('รหัสผ่านไม่ถูกต้อง');
            passwordIsCorrect = true;
          }else{
            $('#password').parent().parent().removeAttr('class');
            $('#password').parent().parent().addClass('control-group success');
            $('#password').next().html('');
            passwordIsCorrect = true;
          }
        });
        
        $('#confirmPassword').bind('keyup',function(){
          if($('#confirmPassword').val() == $('#password').val()){
            $('#confirmPassword').parent().parent().removeAttr('class');
            $('#confirmPassword').parent().parent().addClass('control-group success');
            $('#confirmPassword').next().html('');
            cormfirmPassIsCorrect = true;
          }else{
            $('#confirmPassword').parent().parent().removeAttr('class');
            $('#confirmPassword').parent().parent().addClass('control-group error');
            $('#confirmPassword').next().html('การยืนยันรหัสผ่านไม่ถูกต้อง');
            cormfirmPassIsCorrect = false;
          }
        });
        

        $('#btnCreate').bind('click', function() {
          var haveBlank = false;
          $('form').find('input').each(function(){
            //Check Null
            if($(this).val() == "" && $(this).hasClass('require') ){
              $(this).parent().parent().removeAttr('class');
              $(this).parent().parent().addClass('control-group warning');
              var nameInput = $(this).parent().prev().html();
              //alert(nameInput);
              $(this).next().html('กรุณาใส่ '+nameInput);
              haveBlank = true;
            }else{
              $(this).parent().parent().removeClass('warning');
              $(this).next().html('');
            }

          });
          //if see blank stop working loop
          if(haveBlank){
            return;
          }
          //alert(""+usrIsCorrect+firstnameIsCorrect+lastnameIsCorrect+telIsCorrect+passwordIsCorrect+cormfirmPassIsCorrect);

          if (usrIsCorrect == false || firstnameIsCorrect == false || lastnameIsCorrect == false || 
            telIsCorrect == false || passwordIsCorrect == false || cormfirmPassIsCorrect ==false){
            return;
          }
          //alert('zz');

          $("#notify").removeAttr('class');
          //check newpass equal confirm password
          if( $.trim($('#password').val()) != $.trim($('#confirmPassword').val()) ){
            $('#notify').attr('class','alert alert-error');
            $('#notify strong').html('เกิดข้อผิดพลาด!');
            $('#notify span').html('การยืนยันรหัสผ่านไม่ถูกต้อง');
            $('#notify').show();
            return;
          }else{
            //alert('a');
            $('#notify').removeClass('alert-error');
            $('#notify').hide();
          }


          $.ajax({
            type : 'POST',
            url : 'insertEmployee',
            data : { 
              username : $('#username').val(),password : $('#password').val(),confirmPassword : $('#confirmPassword').val(),
            title : $('#title').val(),firstname : $('#firstname').val(),lastname : $('#lastname').val(), 
            position : $('#position').val(),department : $('#department').val(),subdepartment : $('#subdepartment').val(),
            tel : $('#tel').val(),role : $('#role').val(),status : $('#status').val() },
            success:function(result){
              if(result.indexOf('success') > -1){
                window.location.href = "<?php echo site_url('employeeController'); ?>";
              }
            },
            error:function(err){
              alert('Error : '+err);
            }
          });

        });
      });
      </script>

      <div id="notify" class="alert alert-error" style="display:none;">
        <a class="close" onclick="$('.alert').hide()">&times;</a>  
        <strong></strong> <span></span>
      </div>

      <div>
        
        <form class="form-horizontal">
          <div class="control-group">
            <label class="control-label" for="username" >ชื่อในระบบ *</label>
            <div class="controls">
              <input type="text" id="username" class="require" placeholder="ชื่อที่ใช้ในระบบ" >
              <span class="help-inline"></span>
            </div>
          </div>
          <div class="control-group">
            <label class="control-label" for="password">รหัสผ่าน *</label>
            <div class="controls">
              <input type="password" id="password" class="require" placeholder="Password">
              <span class="help-inline"></span>
            </div>
          </div>
          <div class="control-group">
            <label class="control-label" for="confirmPassword">ยืนยันรหัสผ่าน *</label>
            <div class="controls">
              <input type="password" id="confirmPassword" class="require" placeholder="Password">
              <span class="help-inline"></span>
            </div>
          </div>

          <hr>

          <div class="control-group">
            <label class="control-label" for="title">คำนำหน้าชื่อ</label>
            <div class="controls">
              <select id="title">
                <?php  
                if(count($resultTitle) > 0){
                  foreach ($resultTitle as $row) {
                    echo "<option value='".$row['ref_code']."''>";
                    echo iconv('TIS-620', 'UTF-8', $row['ref_name']); 
                    echo "</option>";
                  }
                }else{
                  echo "<option>ไม่มีข้อมูล</option>";
                }
                ?>
              </select>
              <span class="help-inline"></span>
            </div>

          </div>

          

          <div class="control-group">
            <label class="control-label" for="firstname">ชื่อ *</label>
            <div class="controls">
              <input type="text" id="firstname" class="require" placeholder="ชื่อ">
              <span class="help-inline"></span>
            </div>
          </div>
          <div class="control-group">
            <label class="control-label" for="lastname">นามสกุล *</label>
            <div class="controls">
              <input type="text" id="lastname" class="require" placeholder="นามสกุล">
              <span class="help-inline"></span>
            </div>
          </div>

          <div class="control-group">
            <label class="control-label" for="position">ตำแหน่ง</label>
            <div class="controls">
              <select id="position">
                <?php  
                if(count($resultPosition) > 0){
                  foreach ($resultPosition as $row) {

                    echo "<option value='".$row['ref_code']."''>";
                    echo iconv('TIS-620', 'UTF-8', $row['ref_name']); 
                    echo "</option>";
                  }
                }else{
                  echo "<option>ไม่มีข้อมูล</option>";
                }
                ?>
              </select>
              <span class="help-inline"></span>
            </div>
          </div>

          <div class="control-group">
            <label class="control-label" for="department">กอง</label>
            <div class="controls">
              <select id="department">
              <?php  
                if(count($resultDepartment) > 0){
                  foreach ($resultDepartment as $row) {

                    echo "<option value='".$row['ref_code']."''>";
                    echo iconv('TIS-620', 'UTF-8', $row['ref_name']); 
                    echo "</option>";
                  }
                }else{
                  echo "<option>ไม่มีข้อมูล</option>";
                }
              ?>
              </select>
              <span class="help-inline"></span>
            </div>
          </div>
          <div class="control-group">
            <label class="control-label" for="subdepartment">กองย่อย</label>
            <div class="controls">
              <select id="subdepartment">
              <?php  
                if(count($resultSubdepartment) > 0){
                  foreach ($resultSubdepartment as $row) {
                    echo "<option value='".$row['ref_code']."''>";
                    echo iconv('TIS-620', 'UTF-8', $row['ref_name']); 
                    echo "</option>";
                  }
                }else{
                  echo "<option>ไม่มีข้อมูล</option>";
                }
              ?>
              </select>
              <span class="help-inline"></span>
            </div>
          </div>
          <div class="control-group">
            <label class="control-label" for="tel">เบอร์โทรศัพท์</label>
            <div class="controls">
              <input type="text" id="tel" placeholder="เบอร์โทรศัพท์" maxlength="10">
              <span class="help-inline"></span>
            </div>
          </div>
          <div class="control-group">
            <label class="control-label" for="role">Role</label>
            <div class="controls">
              <select id="role">
              <?php  
                if(count($resultRole) > 0){
                  foreach ($resultRole as $row) {

                    echo "<option value='".$row['role_id']."''>";
                    echo iconv('TIS-620', 'UTF-8', $row['role_name']); 
                    echo "</option>";
                  }
                }else{
                  echo "<option>ไม่มีข้อมูล</option>";
                }
              ?>
              </select>
              <span class="help-inline"></span>
            </div>
          </div>
          <div class="control-group">
            <label class="control-label" for="status">สถานะ</label>
            <div class="controls">
              <select id='status'>
                <option value="A">Active</option>
                <option value="N">Inactive</option>
              </select>
              <span class="help-inline"></span>
            </div>
          </div>

          <div class="control-group">
            <label class="control-label" for="amcam">ชื่อในระบบ Amcam</label>
            <div class="controls">
              <input type="text" id="amcam" placeholder="ชื่อในระบบ Amcam">
              <span class="help-inline"></span>
            </div>
          </div>
          <div class="control-group">
            <label class="control-label" for="amcats">ชื่อในระบบ Amcats</label>
            <div class="controls">
              <input type="text" id="amcats" placeholder="ชื่อในระบบ Amcats">
              <span class="help-inline"></span>
            </div>
          </div>


          <div class="control-group">
            <div class="controls">
              <button type="reset" class="btn">ล้างค่า</button>
              <input id="btnCreate" name="btnCreate" type="button" class="btn btn-primary" value="สร้าง">
            </div>
          </div>
        </form>
      </div>

    </section>
